package com.imodia.checkoo.velocity;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.ui.velocity.VelocityEngineUtils;

public class VelocityService {

	private static Log log = LogFactory.getLog(VelocityService.class);

//	private static Properties getProperties() {
//		Properties properties = new Properties();
//		String path = VelocityService.class.getResource("/").getPath()
//				.replaceAll("^file:/", "").replaceAll("WEB-INF/classes/", "");
//		log.debug("________" + path);
//		properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
//		return properties;
//	}

	private VelocityEngine velocityEngine;

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	public String getString(String vm, Map<String, Object> m) {
		String text = "";
		try {
			log.debug("________vm:" + vm);
			text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,vm, "GBK", m);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//log.debug(text);
		return text;
	}

	public String getString(String vm, String[] key, Object[] o) {
		Map<String, Object> m = new HashMap<String, Object>();

		for (int i = 0; i < key.length && i < o.length; i++) {
			m.put(key[i], o[i]);
		}
		return getString(vm, m);
	}

	public String getString(String vm, String key, Object o) {
		Map<String, Object> m = new HashMap<String, Object>();
		m.put(key, o);
		return getString(vm, m);
	}

	// public static void main(String[] args) {
	// List list = new ArrayList();
	// for (int i = 0; i < 10; i++) {
	// list.add(i);
	// }
	// String templateFile = "mytemplate.vm";
	// Map m = new HashMap();
	// m.put("name", list);
	// try {
	// VelocityUtil.getString(templateFile,m);
	// } catch (ResourceNotFoundException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (ParseErrorException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

}
